#include "pr/Tester.h"
#include "pr/Classifier.h"
#include "pr/TrainingSet.h"

namespace pr {

Tester::Tester(Classifier& classifier, const TrainingSet& testSet)
  : classifier_(classifier)
  , testSet_(testSet)
  , result_(testSet.patternCount())
{
}

void Tester::test()
{
  result_.clear();
  for (std::size_t i = 0; i < testSet_.patternCount(); ++i)
  {
    std::size_t computedClassId = 0;
    const UniquePattern& testPattern = testSet_.getPattern(i);
    if ((computedClassId = classifier_.classify(testPattern.Value)) != testPattern.ClassId)
      result_.addError(testPattern, computedClassId);
  }
}

void Tester::rank(std::size_t rank)
{
  result_.clear();
  for (std::size_t i = 0; i < testSet_.patternCount(); ++i)
  {
    std::size_t computedClassId = 0;
    const UniquePattern& testPattern = testSet_.getPattern(i);
    if (!classifier_.rank(testPattern.Value, testPattern.ClassId, rank))
      result_.addError(testPattern, computedClassId);
  }
}

} // namespace pr
